﻿using System;
using System.Data;
using Vinamilk.DAL;
using HocLapTrinhWeb.DAL;

namespace Vinamilk.BLL
{
    public class v_VinamilkBLL : t_VinamilkBLL
    {
        #region Variable
        ClassBaseDAL _ClassBaseDAL;
        #endregion

        #region Public Methods

        /// <summary>
        /// 
        /// </summary>
        /// <param name="conn"></param>
        public v_VinamilkBLL(Connection conn)
            : base(conn)
        {

        }

        public vwVinamilk.vw_VinamilkDataTable GetAllVinamilkForGridView(int startRowIndex, int maximumRows)
        {
            var isOpen = false;
            try
            {
                if (OpenConnection(ref isOpen))
                {
                    var dt = new vwVinamilk.vw_VinamilkDataTable();
                    _ClassBaseDAL = new ClassBaseDAL(IConnect, dt)
                    {
                        StartRow = startRowIndex,
                        MaxRows = maximumRows,
                        OrderByClause = dt.VinamilkIDColumn.ColumnName + " desc"
                    };
                    if (_ClassBaseDAL.FillData(dt))
                        return dt;
                    AddMessage("ERR-000006", "Tải dữ liệu không thành công." + _ClassBaseDAL.getMessage(), _ClassBaseDAL.getMsgNumber());
                    return null;
                }
                AddMessage("ERR-000001", "Connection failed." + getMessage(), 0);
                return null;
            }
            catch (Exception ex)
            {
                AddMessage("ERR-000006", "Tải dữ liệu không thành công." + ex.Message, 0);
                return null;
            }
            finally
            {
                CloseConnection(isOpen);
            }
        }

        public int GetAllVinamilkRowCount()
        {
            var isOpen = false;
            try
            {
                if (OpenConnection(ref isOpen))
                {
                    var dt = new vwVinamilk.vw_VinamilkDataTable();
                    _ClassBaseDAL = new ClassBaseDAL(IConnect, dt);
                    var rowcount = _ClassBaseDAL.GetRowCount();
                    if (rowcount == -1)
                        AddMessage("ERR-000006", "Tải dữ liệu không thành công." + _ClassBaseDAL.getMessage(), _ClassBaseDAL.getMsgNumber());
                    return rowcount;
                }
                AddMessage("ERR-000001", "Connection failed." + getMessage(), 0);
                return -1;
            }
            catch (Exception ex)
            {
                AddMessage("ERR-000006", "Tải dữ liệu không thành công." + ex.Message, 0);
                return -1;
            }
            finally
            {
                CloseConnection(isOpen);
            }
        }


        public vwVinamilk.vw_VinamilkDataTable GetAllVinamilkForGridView(int startRowIndex, int maximumRows, int ProvinceID, int VinamilkTypeID, int IsActive)
        {
            var isOpen = false;
            try
            {
                if (OpenConnection(ref isOpen))
                {
                    var dt = new vwVinamilk.vw_VinamilkDataTable();
                    _ClassBaseDAL = new ClassBaseDAL(IConnect, dt)
                    {
                        StartRow = startRowIndex,
                        MaxRows = maximumRows,
                        OrderByClause = dt.VinamilkIDColumn.ColumnName + " desc"
                    };
                    _ClassBaseDAL.WhereClause = " 1 = 1 ";
                    if (ProvinceID != -1)
                    {
                        _ClassBaseDAL.WhereClause += " and " + dt.ProvinceIDColumn.ColumnName + "=@ProvinceID";
                        _ClassBaseDAL.AddParams("@ProvinceID", SqlDbType.Int, ProvinceID, ParameterDirection.Input);
                    }
                    if (VinamilkTypeID != -1)
                    {
                        _ClassBaseDAL.WhereClause += " and " + dt.VinamilkTypeIDColumn.ColumnName + "=@VinamilkTypeID";
                        _ClassBaseDAL.AddParams("@VinamilkTypeID", SqlDbType.Int, VinamilkTypeID, ParameterDirection.Input);
                    }
                    if (IsActive != -1)
                    {
                        _ClassBaseDAL.WhereClause += " and " + dt.IsActiveColumn.ColumnName + "=@IsActive";
                        _ClassBaseDAL.AddParams("@IsActive", SqlDbType.Int, IsActive, ParameterDirection.Input);
                    }
                    if (_ClassBaseDAL.FillData(dt))
                        return dt;
                    AddMessage("ERR-000006", "Tải dữ liệu không thành công." + _ClassBaseDAL.getMessage(), _ClassBaseDAL.getMsgNumber());
                    return null;
                }
                AddMessage("ERR-000001", "Connection failed." + getMessage(), 0);
                return null;
            }
            catch (Exception ex)
            {
                AddMessage("ERR-000006", "Tải dữ liệu không thành công." + ex.Message, 0);
                return null;
            }
            finally
            {
                CloseConnection(isOpen);
            }
        }


        public vwVinamilk.vw_VinamilkDataTable ThuKiemTra( int IsActive)
        {
            var isOpen = false;
            try
            {
                if (OpenConnection(ref isOpen))
                {
                    var dt = new vwVinamilk.vw_VinamilkDataTable();
                    _ClassBaseDAL = new ClassBaseDAL(IConnect, dt);
                    _ClassBaseDAL.WhereClause = " 1 = 1 ";
                    if (IsActive != -1)
                    {
                        _ClassBaseDAL.WhereClause += " and " + dt.IsActiveColumn.ColumnName + "=@IsActive";
                        _ClassBaseDAL.AddParams("@IsActive", SqlDbType.Int, IsActive, ParameterDirection.Input);
                    }
                    if (_ClassBaseDAL.FillData(dt))
                        return dt;
                    AddMessage("ERR-000006", "Tải dữ liệu không thành công." + _ClassBaseDAL.getMessage(), _ClassBaseDAL.getMsgNumber());
                    return null;
                }
                AddMessage("ERR-000001", "Connection failed." + getMessage(), 0);
                return null;
            }
            catch (Exception ex)
            {
                AddMessage("ERR-000006", "Tải dữ liệu không thành công." + ex.Message, 0);
                return null;
            }
            finally
            {
                CloseConnection(isOpen);
            }
        }

        public int GetAllVinamilkRowCount(int ProvinceID, int VinamilkTypeID, int IsActive)
        {
            var isOpen = false;
            try
            {
                if (OpenConnection(ref isOpen))
                {
                    var dt = new vwVinamilk.vw_VinamilkDataTable();
                    _ClassBaseDAL = new ClassBaseDAL(IConnect, dt);
                    _ClassBaseDAL.WhereClause = " 1 = 1 ";
                    if (ProvinceID != -1)
                    {
                        _ClassBaseDAL.WhereClause += " and " + dt.ProvinceIDColumn.ColumnName + "=@ProvinceID";
                        _ClassBaseDAL.AddParams("@ProvinceID", SqlDbType.Int, ProvinceID, ParameterDirection.Input);
                    }
                    if (VinamilkTypeID != -1)
                    {
                        _ClassBaseDAL.WhereClause += " and " + dt.VinamilkTypeIDColumn.ColumnName + "=@VinamilkTypeID";
                        _ClassBaseDAL.AddParams("@VinamilkTypeID", SqlDbType.Int, VinamilkTypeID, ParameterDirection.Input);
                    }
                    if (IsActive != -1)
                    {
                        _ClassBaseDAL.WhereClause += " and " + dt.IsActiveColumn.ColumnName + "=@IsActive";
                        _ClassBaseDAL.AddParams("@IsActive", SqlDbType.Int, IsActive, ParameterDirection.Input);
                    }
                    var rowcount = _ClassBaseDAL.GetRowCount();
                    if (rowcount == -1)
                        AddMessage("ERR-000006", "Tải dữ liệu không thành công." + _ClassBaseDAL.getMessage(), _ClassBaseDAL.getMsgNumber());
                    return rowcount;
                }
                AddMessage("ERR-000001", "Connection failed." + getMessage(), 0);
                return -1;
            }
            catch (Exception ex)
            {
                AddMessage("ERR-000006", "Tải dữ liệu không thành công." + ex.Message, 0);
                return -1;
            }
            finally
            {
                CloseConnection(isOpen);
            }
        }

        public vwVinamilk.vw_VinamilkDataTable GetAllVinamilkForGridView(int startRowIndex, int maximumRows, string txtSearch, int IsActive)
        {
            var isOpen = false;
            try
            {
                if (OpenConnection(ref isOpen))
                {
                    var dt = new vwVinamilk.vw_VinamilkDataTable();
                    _ClassBaseDAL = new ClassBaseDAL(IConnect, dt)
                    {
                        StartRow = startRowIndex,
                        MaxRows = maximumRows,
                        OrderByClause = dt.VinamilkIDColumn.ColumnName + " desc"
                    };
                    _ClassBaseDAL.WhereClause = " 1 = 1 ";
                    if (txtSearch != "")
                    {
                        _ClassBaseDAL.WhereClause += " and (" + dt.AddressColumn.ColumnName + " like N'%" + txtSearch + "%' OR " + dt.VinamilkNameColumn.ColumnName + " like N'%" + txtSearch + "%' )";
                    }
                    if (IsActive != -1)
                    {
                        _ClassBaseDAL.WhereClause += " and " + dt.IsActiveColumn.ColumnName + "=@IsActive";
                        _ClassBaseDAL.AddParams("@IsActive", SqlDbType.Int, IsActive, ParameterDirection.Input);
                    }
                    if (_ClassBaseDAL.FillData(dt))
                        return dt;
                    AddMessage("ERR-000006", "Tải dữ liệu không thành công." + _ClassBaseDAL.getMessage(), _ClassBaseDAL.getMsgNumber());
                    return null;
                }
                AddMessage("ERR-000001", "Connection failed." + getMessage(), 0);
                return null;
            }
            catch (Exception ex)
            {
                AddMessage("ERR-000006", "Tải dữ liệu không thành công." + ex.Message, 0);
                return null;
            }
            finally
            {
                CloseConnection(isOpen);
            }
        }



        #endregion

    }
}
